#ifndef _XLAYOUT1_H
#define _XLAYOUT1_H

#include <QObject>
#include <qstring.h>
#include <qvector.h>
#include <qdatetime.h>
#include <qtimer.h>
#include <qfile.h>
#include <xwritelogexport.h>
#include "infomatcommon.h"
#include "xdatabootconf.h"

class XHtmlPlayer;
class XMoviePlayer;

class XLayout1 : public QObject
{
	Q_OBJECT

public:
	XLayout1(QObject *parent=0);
	~XLayout1();
	
	/* Event Process */	
//	int  doRoutine(bool needSwitchPartial=false);
//	Caller has to indicate the screen Mode, including Partial Screen or Full Screen.
	int  doRoutine(int screenMode);
	int  doPlu(const int plu, bool needSwitchPartial=false);
	int  doEnterScreenSaver();
	int  doExitScreenSaver();
	int  doEnterMenuMode();
	int  doExitMenuMode();
	int  doHalt();
	int  doResume();

private slots:
	void onCPlayerIdle();
	void onPlayHtmlRoutineDone(int);
	void onPlayHtmlPluDone(int);
	void onPlayMovieRoutineDone(int);
	void onPlayMoviePluDone(int);

signals:
	void sgUpdateHtmlList(int id);
	void sgUpdateMovieList(int id);
	void sgWMRepaint(); // To MainWidget

protected:
	void setScreenMode(int mode);
	int  updateRoutineList();
	int  buildRoutineList();
	void clearHtmlRoutinePlayConnections();
	void clearMovieRoutinePlayConnections();
	void clearHtmlPluPlayConnections();
	void clearMoviePluPlayConnections();
	void processMovieEvents();
	int  isPluValid(const int plu);
	int  buildPluList(const int plu);
	int  doMovieRoutinePlay();  //stop players and check files
	void startMovieRoutinePlay();
	int  startHtmlRoutinePlay(int mode=EPartialScreen);
	void checkList();
	void startHtmlPlu();
	void startMoviePluPlay();

private:
	XHtmlPlayer  *m_xHtmlPlayer;
	XMoviePlayer *m_xMoviePlayer;
	
	int    m_event;
	QTimer m_tmCheckPlayers;
	QVector<STFileInfo>  m_htmlRoutineList, m_htmlPluList;
	QVector<STFileInfo>  m_movieRoutineList, m_moviePluList;
	QVector<STFileInfo>  m_nextHtmlPluList, m_nextMoviePluList;
	int    m_htmlRoutineIndex, m_htmlPluIndex, m_movieRoutineIndex, m_moviePluIndex;
	QDateTime m_routineListLastModified;
	QTimer m_tmRoutineList;   //timer to control update routine list
	bool   m_isHtmlIdle, m_isCPlayerIdle;
	bool   m_isNextPluHtml, m_isNextPluMovie;
	bool   m_isPlayingMoviePlu;
	bool   m_initHtmlWithFullScreen;
	bool   m_initMovieWithFullScreen;
	bool   m_needSwitchToParitalScreen;
	int   m_currentPlu;
	int   m_screenMode;
	XDataBootConf m_xbootconf;

protected:
	inline bool isPicture(const char* in_file)  {
		assert (NULL!=in_file);
		QString file = QString(in_file);
		if (file.endsWith("JPEG") || file.endsWith("jpeg") ||
			file.endsWith("JPG")  || file.endsWith("jpg")  ||
			file.endsWith("BMP")  || file.endsWith("bmp")  ||
			file.endsWith("GIF")  || file.endsWith("gif")  ||
			file.endsWith("PNG")  || file.endsWith("png") )
			return true;
		else
			return false;
	}
	enum EUpdateList {
		EUpdateRoutineList = -10,
		EUpdatePluList,
		EFileNotExist
	};
	enum EScreenMode {
		EScreenModeOff = 0,
		EScreenModeOn,
	};
	enum EPluValid {
		EInvalidValue = -1,
		EBothPluValid,
		EHtmlPluValidOnly,
		EMoviePluValidOnly,
	};
	enum EScreenSize {
		EFullScreen = 0,
		EPartialScreen,
	};

	inline bool isFileExist(const char* in_file) {
		bool isExist = false;
		if (!QFile::exists(QString(in_file))) {
			LOG_E("file:%s is not existed", in_file);
			isExist = false;
		} else {
//			LOG_T("file:%s is existed", in_file);
			isExist = true;
		}
		return isExist;
	}
	enum EInfomatEvents {
		ENoEvent = -1,
		ERoutine,   			//0
		ERoutineWithPartialSceen,
		EPlu,
		EQueue,
		EEnterMenuMode,
		EExitMenuMode,
		EEnterScreenSaver,    	//5
		EExitScreenSaver,
	};
};
#endif
